package com.example.nacosclientrabbit.receiver;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;


@Slf4j
@Component
public class Receiver {
    @RabbitListener(queues = {"pc"})
    private void listener(String msg, Channel channel, Message message){
        long deliverTag = message.getMessageProperties().getDeliveryTag();
        try {
            Map<String, Object> map = JSON.parseObject(msg, Map.class);
            log.info("firstNodeListener - 消费消息 [" + deliverTag + "] - " + map);
            channel.basicAck(deliverTag, true);
        } catch (Exception ex) {
            try {
                channel.basicNack(1000, false, false);
            } catch (IOException e) {
                log.error(ex.getMessage());
            }
                log.error(ex.getMessage());
        }
    }
}
